﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.member
{
    public partial class message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(message));
            var userInfo = WebMasterCookie.GetCookie();
            if (!IsPostBack)
            {
                if (userInfo == null)
                {
                    Response.Redirect("/login.aspx");
                }
                var role = Request.QueryString["role"];
                if (role == "my")
                {
                    lbTitle.Text = "我的留言";
                    AspNetPager1.RecordCount = MessageInfoBll.GetMessageInfoCount("Senter_Id=" + userInfo.Id + "",
                                                                                  CommonConst.DB_Name);
                }
                else if (role == "index")
                {
                    AspNetPager1.RecordCount = MessageInfoBll.GetMessageInfoCount("Client_Id=" + userInfo.Id + "  and Message_Status=1",
                                                                                  CommonConst.DB_Name);
                }
                else
                {
                    Response.Redirect("/shop/error.html");
                }
            }
        }

        private void BindAllNews()
        {
            var userInfo = WebMasterCookie.GetCookie();
            Dictionary<string, object> dicHot = new Dictionary<string, object>();
            var pageIndex = AspNetPager1.CurrentPageIndex - 1;
            var pageSize = AspNetPager1.PageSize;
            string orderBy = " Message_Date desc";
            var role = Request.QueryString["role"];
            DataTable dt;
            if (role == "my")
            {
                dicHot.Add("Senter_Id", userInfo.Id);
            }
            else
            {
                dicHot.Add("Client_Id", userInfo.Id);
                dicHot.Add("Message_Status", 1);
            }
            dt = MessageInfoBll.GetMessageInfoTable(pageIndex, pageSize, "*", orderBy, dicHot, CommonConst.DB_Name);
            repMsg.DataSource = dt;
            repMsg.DataBind();
        }

        //删除留言
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string onDeleteMsg(string messageId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo != null)
            {
                try
                {
                    MessageInfoBll.DeleteMessageInfo(messageId, userInfo.Id, CommonConst.DB_Name);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", "删除留言");
                    dicHandleLog.Add("obj_guid", "Message_Id=" + messageId);
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", "删除成功！");
                }
                catch (Exception ex)
                {
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
                
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "登录超时，请退出重新登录！");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindAllNews();
        }
    }
}
